﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using WinRTDatabase;

namespace WinRTDb.Tests
{
    [TestClass]
    public class WinRTDBUnitTests
    {
        [TestMethod]
        public async Task DatabaseDoesNotExists()
        {
            string name = Guid.NewGuid().ToString();
            bool exists = await Database.DoesDatabaseExistsAsync(name);
            Assert.IsFalse(exists, "Database should not exist");
        }

        [TestMethod]
        async public Task DatabaseExistsCreateDatabase()
        {
            string name = Guid.NewGuid().ToString();
            Database db = await Database.CreateDatabaseAsync(name);
            db.CreateTable<Person>();
            var result = await db.SaveAsync();
            Assert.IsNull(result.Error, "Database should save");
            bool exists = await Database.DoesDatabaseExistsAsync(name);
            Assert.IsTrue(exists, "database should exist");
        }

        [TestMethod]
        async public Task DeleteDatabase()
        {
            string name = Guid.NewGuid().ToString();
            Database db = await Database.CreateDatabaseAsync(name);
            db.CreateTable<Person>();
            var result = await db.SaveAsync();
            Assert.IsNull(result.Error, "Database should save");
            bool exists = await Database.DoesDatabaseExistsAsync(name);
            Assert.IsTrue(exists, "database should exist");
            var deleteResult = await Database.DeleteDatabase(name);
            Assert.IsNull(deleteResult.Error, "Database should delete");
            exists = await Database.DoesDatabaseExistsAsync(name);
            Assert.IsFalse(exists, "database should not exist");
        }

        [TestMethod]
        async public Task OpenDatabaseWithSavedData()
        {
            string name = Guid.NewGuid().ToString();
            Database db = await Database.CreateDatabaseAsync(name);
            db.CreateTable<Person>();
            var table = await db.Table<Person>();
            table.Add(new Person() { PersonID = "1", FirstName = "First1", LastName = "Last1", Age = 1 });
            table.Add(new Person() { PersonID = "2", FirstName = "First2", LastName = "Last2", Age = 2 });
            var result = await db.SaveAsync();
            Assert.IsNull(result.Error, "Database should save");
            bool exists = await Database.DoesDatabaseExistsAsync(name);
            Assert.IsTrue(exists, "database should exist");
            Database openedDB = await Database.OpenDatabaseAsync(name, false);
            Table<Person> opened = await openedDB.Table<Person>();
            Assert.AreEqual((int)2, opened.Count, "Should have two people");
            Debug.WriteLine(opened.First<Person>().Age);
            Debug.WriteLine(opened.Skip<Person>(1).First().Age);
            var deleteResult = await Database.DeleteDatabase(name);
        }

        [TestMethod]
        async public Task DeleteTwiceTest()
        {
            string name = Guid.NewGuid().ToString();
            var exists = await Database.DoesDatabaseExistsAsync(name);
            if (exists)
                await Database.DeleteDatabase(name);

            var database = await Database.CreateDatabaseAsync(name, StorageLocation.Local);
            database.CreateTable<Person>();
            await database.SaveAsync();
            await Database.DeleteDatabase(name);
            database = await Database.CreateDatabaseAsync(name, StorageLocation.Local);
            var table = await database.Table<Person>();
            await database.SaveAsync();
            await Database.DeleteDatabase(name);

            database = await Database.CreateDatabaseAsync(name, StorageLocation.Local);
            database.CreateTable<Person>();
            await database.SaveAsync();
            await Database.DeleteDatabase(name);
        }
    }
}
